package com.penjin.android.activity.apply;

import android.os.Bundle;
import android.view.View;

import com.loopj.android.http.RequestParams;
import com.penjin.android.utils.DateUtil;
import com.penjin.android.utils.JsonUtils;
import com.penjin.android.utils.ToastHelper;
import com.penjin.android.view.TitleBarView;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/3/24.
 */
public class QingjiaApplyDetailActivity extends BillApplyDetailActivity {


    @Override
    protected void onInitView() {
        this.titleBarView.setTitleBarListener(new TitleBarView.TitleBarListener() {
            @Override
            public void left(View view) {
                QingjiaApplyDetailActivity.this.finish();
            }

            @Override
            public void center(View view) {

            }

            @Override
            public void right(View view) {

            }
        });
        this.titleBarView.setCenterText("请假申请单");
    }

    @Override
    protected void onInitData(Bundle bundle) {

    }

    @Override
    protected RequestParams generateParams() {
        RequestParams params = new RequestParams();
        params.put("staffNumber", staff.getStaffNumber());
        params.put("companyId", company.getCompanyId());
        params.put("billSort", billSort);
        params.put("id", id);
        params.put("billNumber", billNum);
        System.out.println("billNumber="+billNum);
        return params;
    }

    @Override
    protected void onLoadBillDetailSuccess(String s) {
        try {
            JSONObject jo = new JSONObject(s);
            boolean result = jo.optBoolean("result");
            JSONArray dataArray=jo.getJSONArray("data");
            JSONObject data=dataArray.getJSONObject(0);
            if (result) {
                timeName.setText("请假时长共计");
                billNumber.setText(billNum);
                billTypeName.setText(data.optString("billType"));
                type.setText(data.optString("type"));
                String reason = data.optString("reason");
                if (JsonUtils.isJsonNull(reason)) {
                    detail.setText("详情未填写");
                } else
                    detail.setText(jo.optString("reason"));
                String startDate = data.optString("startTime");
                String endDate = data.optString("endTime");
                startTime.setText(DateUtil.toUIDate(startDate));
                endTime.setText(DateUtil.toUIDate(endDate));
                //计算单据一共经历多长时间
                try {
                    timeDays.setText(DateUtil.getBetweenDays(startDate, endDate));
                } catch (Exception e) {
                    e.printStackTrace();
                    //忽略日期解析错误
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ToastHelper.showJsonError(this);
        }
    }
}
